#include <stdio.h>
#include <string.h>
#define MAXN 101 

int n,m,k;

int map[MAXN][MAXN];
int ans;
int cx[MAXN],cy[MAXN],sx[MAXN],sy[MAXN];

int path(int u)
{
	int v;
	int i;

	sx[u]=1;
	for (v=1;v<=m;v++)
		if (map[u][v]==1 && !sy[v])
		{
			sy[v]=1;
			if ( !cy[v] || path(cy[v]))
			{
				cx[u]=v;
				cy[v]=u;
				return 1;
			}
		}
	return 0;
}


void solve()
{
	int i;
	memset(cx,0,sizeof(cx));
	memset(cy,0,sizeof(cy));
	ans=0;
	for (i=1;i<=n;i++)
		if (!cx[i])
		{
			memset(sx,0,sizeof(sx));
			memset(sy,0,sizeof(sy));
			ans+=path(i);
		}


}


main()
{
	int i,j,a,b;
	while (1)
	{
		scanf("%d", &n);
		if (n==0) break;
		scanf("%d%d", &m, &k);
		memset(map,0,sizeof(map));
		for (i=1;i<=k;i++)
		{
			scanf("%d%d%d", &j, &a, &b);
			map[a][b]=1;
		}
		solve();
		printf("%d\n", ans);

	}
	

	return 0;
}